package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.ImageView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.my.fragment.VipFragment;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.view.TabView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/6/16.
 */

public class ToBeVipOrganize extends SlidingActivity {

	@ViewInject(R.id.drawerLayout)
	protected DrawerLayout drawerLayout;
	@ViewInject(R.id.sort_btn)
	protected ImageView sortBtn;
	@ViewInject(R.id.tab_view)
	private TabView tabView;
	@ViewInject(R.id.order_viewpager)
	protected ViewPager viewPager;

	private int position;
	private List<String> tabNames;
	private List<Fragment> fragments;
	private List<OrganizaModel> organizaModels;

	@Override
	protected int getResourceId() {
		return R.layout.activity_order;
	}

	@Override
	protected void init() {
		position = getIntent().getIntExtra("position", -1);
		topName.setText("VIP企业套餐");
		rightIcon.setVisibility(View.GONE);
		leftIcon.setImageResource(R.mipmap.arrow_white_left);
		leftIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		sortBtn.setVisibility(View.GONE);
		drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
		tabNames = new ArrayList<>();
		fragments = new ArrayList<>();
		initFragment();
		initTabLayout();
		viewPager.setBackgroundColor(mContext.getResources().getColor(R.color.white));
		viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public Fragment getItem(int position) {
				return fragments.get(position);
			}
			@Override
			public int getCount() {
				return fragments.size();
			}
		});
		tabView.setTabItems(tabNames);
		tabView.setupWithViewPager(viewPager);
		if(position != -1) {
			viewPager.setCurrentItem(position);
		} else{
			String companyId = getIntent().getStringExtra("companyId");
			if(companyId == null)
				return;
			for(int i = 0; 0 < organizaModels.size(); i++) {
				if (companyId.equals(organizaModels.get(i).getCompanyId())) {
					viewPager.setCurrentItem(i);
					break;
				}
			}
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		String companyId = intent.getStringExtra("companyId");
		if(companyId == null)
			return;
		for(int i = 0; 0 < organizaModels.size(); i++) {
			if (companyId.equals(organizaModels.get(i).getCompanyId())) {
				viewPager.setCurrentItem(i);
				break;
			}
		}
	}

	private void initFragment(){
		organizaModels = getCompanyList();
		for(OrganizaModel organizaModel : organizaModels)
			fragments.add(VipFragment.newInstance(1, organizaModel));
	}

	private void initTabLayout(){
		tabNames.clear();
		for(OrganizaModel organizaModel : organizaModels)
			tabNames.add(organizaModel.getCompanyName());
	}

	private List<OrganizaModel> getCompanyList(){
		List<OrganizaModel> companyList = new ArrayList<>();
		UserData userData = application.getUserData();
		if(userData == null)
			return companyList;
		List<OrganizaModel> allCompanyList = userData.getCompanyList();
		if(allCompanyList != null && allCompanyList.size() > 0) {
			for (OrganizaModel company : allCompanyList){
				if(company.getCompanyState() != 1 || (company.getAuditStete() != 0 && company.getAuditStete() != 2))
					companyList.add(company);
			}
		}
		return  companyList;
	}
}
